package com.baiku.sanhos.controller;

import com.baiku.sanhos.mapper.AppointMapper;
import com.baiku.sanhos.pojo.dto.AppointDTO;
import com.baiku.sanhos.pojo.vo.AppointDetailVO;
import com.baiku.sanhos.pojo.vo.FutureWeekendDateVO;
import com.baiku.sanhos.response.RequestData;
import com.baiku.sanhos.response.ResultVO;
import com.baiku.sanhos.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

@Slf4j
@Api(tags = "排班管理")
@RestController
@RequestMapping("/pa/appoint")
public class AppointController {
    @Autowired
    private AppointMapper appointMapper;

    /**
     * 根据科室ID查询近一周的排班日期
     * @param deptId 科室id
     * @return 一周的排版数据
     */
    @ApiOperation("根据科室id查询一周排班")
    @GetMapping("byDeptId/{deptId:[0-9]+}")
    public ResultVO getFutureWeekendDateByDeptId(@PathVariable Integer deptId){
        List<FutureWeekendDateVO> weekendDateVOList = appointMapper.getFutureWeekendDateByDeptId(deptId);
        if (weekendDateVOList.isEmpty()){
            return new ResultVO(StatusCode.DATA_NOT_FOUND);
        }
        return ResultVO.ok(weekendDateVOList);
    }

    /**
     * 根据科室ID查询近一周的排班日期
     * @param doctorNo 医生工号
     * @return 一周的排版数据
     */
    @ApiOperation("根据医生工号查询一周排班")
    @GetMapping("byDoctorNo/{doctorNo}")
    public ResultVO getFutureWeekendDateByDoctorNo(@PathVariable String doctorNo){
        List<FutureWeekendDateVO> weekendDateVOList = appointMapper.getFutureWeekendDateByDoctorNo(doctorNo);
        if (weekendDateVOList.isEmpty()){
            return new ResultVO(StatusCode.DATA_NOT_FOUND);
        }
        return ResultVO.ok(weekendDateVOList);
    }

    /**
     * 医生排班
     */
    @GetMapping("/selectSchedule")
    public ResultVO selectSchedule(@RequestParam("page") int page,
                                   @RequestParam("pageSize") int pageSize,
                                   @RequestParam("scheduleTime") String scheduleTime){
        if(scheduleTime.equals("")){
            Map<String,Object> map=new HashMap<>();
            int total=appointMapper.getTotalAppoints(scheduleTime);//总条数
            int totalPages=(int)Math.ceil((double) total/pageSize);//计算总页数
            List<AppointDetailVO>  appointDetails;
            if(totalPages==0){
                appointDetails=new ArrayList<>();
            }else {
                int offset=(page-1)*pageSize;
                appointDetails=appointMapper.selectAllAppoint(pageSize,offset,scheduleTime);

            }
            map.put("appointDetails",appointDetails);
            map.put("total",total);
            return ResultVO.ok(map);
        }
        List<AppointDetailVO>  appointDetailVOS=appointMapper.selectAllAppointScheduleTime(scheduleTime);
        if(appointDetailVOS.size()==0){
            return new ResultVO(1007,"数据不在");
        }
        return ResultVO.ok(appointDetailVOS);
    }

    /**
     * 根据id删除排班信息
     * @param id
     * @return
     */
    @ApiOperation("根据id删除排班信息")
    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable Integer id){
        appointMapper.deleteById(id);
        return ResultVO.ok();
    }

    /**
     * 根据id编辑排班信息
     * @param appointDTO
     * @return
     */
    @ApiOperation("根据id编辑排班信息")
    @PutMapping("/updateById")
    public ResultVO updateById(@RequestBody AppointDTO appointDTO){
        appointDTO.setSourceSurplus(appointDTO.getSourceTotal());
        appointMapper.updateAppointById(appointDTO);
        return ResultVO.ok();
    }

    /**
     * 根据ids批量修改发布状态
     * @param requestData
     * @return
     */
    @ApiOperation("根据ids批量修改发布状态")
    @PutMapping("/updateByIds")
    public ResultVO updateByIds(@RequestBody RequestData requestData){
        List<Integer> ids = requestData.getData();
        appointMapper.updateByIds(ids);
        return ResultVO.ok();
    }

    /**
     *新增医生的排班日期
     * @param appointDTO
     * @return
     */
    @ApiOperation("新增医生的排班日期")
    @PostMapping("/addAppoint")
    public ResultVO addAppoint(@RequestBody AppointDTO appointDTO){
        Date schedulTime = appointDTO.getSchedulTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式        appointMapper.addAppoint(appointDTO);
        String dateString = dateFormat.format(schedulTime); // 将 Date 对象格式化为字符串
        LocalDate localDate = LocalDate.parse(dateString); // 将字符串解析为 LocalDate 对象
        LocalDate newDate = localDate.plusDays(1); // 在日期上加一天
        ZoneId zoneId = ZoneId.systemDefault(); // 获取系统默认时区
        Date schedultime = Date.from(newDate.atStartOfDay(zoneId).toInstant()); // 将 LocalDate 转换为 Date
        appointDTO.setSchedulTime(schedultime);
        int total = appointMapper.queryOneFindByTime(appointDTO);
        if(total>0){
            return ResultVO.error(StatusCode.SCHEDULING_EXISTS);
        }
        appointDTO.setSourceSurplus(appointDTO.getSourceTotal());
        appointMapper.addAppoint(appointDTO);
        return ResultVO.ok();
    }
}
